<template>
  <!-- 训练营证书 -->
  <div>
    <camp-certifi-com @canvasOver="canvasOver" ref="campcate" :prodType='prodType' :bigType="bigType" :prodId='prodId' :showCertificate="showCertificate" :certInfo='certificateInfo' @updateImgeUrl="updateImgeUrl" @closeCertificate='closeCertificate'>
    </camp-certifi-com>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js";
import campCertifiCom from "./campCertifiCom.vue";

// let campId = ''; //当前课程ID
export default {
  name: "CampCertificate",
  components: {
    campCertifiCom
  },
  props: {
    bigType: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : "mb5_default",
      showCertificate: false,
      certificateInfo: {},
      prodType:'',
      prodId:'',
      topicId:''
    };
  },
  created() {
    utilJs.appShare(this);
  },
  mounted() { },
  methods: {
    canvasOver() {
      this.$loading.hide();
    },
    closeCertificate() {
      this.showCertificate = false;
    },
    showCerImage(dataURL) {
      this.$nextTick(() => {
        this.$refs.campcate.showCerImage(dataURL)
      });
    },
    updateImgeUrl(image) {
      this.$emit('updateImgeUrl', image);
    },
    //获取我的证书信息
    getMyCert(topicId, prodId, prodType) {
      this.prodType = prodType,
      this.prodId = prodId
      this.topicId = topicId
      this.$loading.show({
        text: "证书生成中"
      });
      let postUrl = `${global.apiurl}Certificate/cardShow?prodType=${global.ckFrom.camp}&prodId=${prodId}+ '&type=1'`
      utilJs.getMethod(postUrl, res => {
        this.showCertificate = true;
        this.showRepPop = false;
        this.campName = res.name;
        let title = res.prodName;
        let titleT = '【' + res.prodName + '】';
        let titleArr = [];
        if (titleT.length > 0 && titleT.length <= 18) {
          let title1 = titleT.substring(0, 18);
          titleArr.push(title1);
        } else if (titleT.length > 18) {
          let title1 = titleT.substring(0, 18);
          let title2 = "";
          if (titleT.length <= 36) {
            title2 = titleT.substring(18, 36);
          } else {
            title2 = titleT.substring(18, 34);
            title2 = title2 + "...】";
          }
          titleArr.push(title1);
          titleArr.push(title2);
        }

        let qrcodeUrl = "";
        let qrText = "";
        // "redirectTo" 跳转，1：详情页，2：首页
        let redirectQr = "";
        if (res.redirectTo == 1) {
          redirectQr = res.trainingBaseInfo.qrCodeUrl;
          qrText = "进入训练营";
        } else if (res.redirectTo == 2) {
          redirectQr = res.trainingBaseInfo.homeQrcode;
          qrText = "进入首页"
        } else {
          redirectQr = res.customizeQrCode.img;
        }
        qrText = res.qrCodeText ? res.qrCodeText : qrText
        if (redirectQr.includes("?")) {
          qrcodeUrl = redirectQr + "&time=" + new Date().getTime();
        } else {
          qrcodeUrl = redirectQr + "?time=" + new Date().getTime();
        }
        let imgurl2 = 'https://kposshzcdn.ckjr001.com/2087/resource/admin-fe_jzr8_TcMTRdPGYzjj3QQA6kNw.png'
        if (res.image == imgurl2) {
          res.backdropId = 3
        }

        this.certificateInfo = {
          bgImgUrl: res.image,
          headimgurl: res.headimgurl || require('@/assets/defult_head.png'),
          nameTypeShow: res.nameTypeShow,
          nickname: res.nickname,
          realName: res.realName,
          name: res.prodName,
          nameText: title, //去掉符号的营期名
          qrcodeUrl: qrcodeUrl,
          qrText: qrText,
          backdropId: res.backdropId,
          contentType: res.contentType,
          word: res.content || [],
          certificateType: res.certificateType,
          defineShowContent:res.defineShowContent,
          enableAvatarShow:res.enableAvatarShow,
          finishCourseNum: res.trainingBaseInfo.finishCourseNum,
          finishExamNum: res.trainingBaseInfo.finishExamNum,
          finishExamTotalScore: res.trainingBaseInfo.finishExamTotalScore,
          finishSignNum: res.trainingBaseInfo.finishSignNum,
          finishTaskNum: res.trainingBaseInfo.finishTaskNum,
          customizeControl:res.customizeControl,
          customizeImage:res.customizeImage,
          createdAt:res.createdAt,
          shareDesc:res.shareDesc,
          enableShowQrCode:res.enableShowQrCode,
          title:res.title,
          enableInstitution:res.enableInstitution, //是否展示发证机构：1=展示；0=隐藏
          institutionName:res.institutionName, //机构名称
          enableGrantTime:res.enableGrantTime, //是否展示发证时间  1=展示；0=隐藏
          enableNumber:res.enableNumber, //是否展示证书编号 1=展示；0=隐藏
          number:res.number, //起始编号
          enableStamp:res.enableStamp, //是否展示章戳  1=展示；2=自定义; 0=隐藏
          stampImg:res.stampImg, //章戳自定义图片
          shapeType:res.shapeType,
        };
        console.log(this.certificateInfo)
        this.wechatShare();
      },
        failRes => {
          this.$loading.hide();
        });
    },

    //分享
    wechatShare: function () {
      let title = "我的成就";
      let shareUrl = `${window.location.href.split("#")[0]}#/member/myAchievement/AchievementList?refereeId=${localStorage.getItem("userId")}`;
      utilJs.wechatConfig(shareUrl, title, "", "", function () { }, true);
    },
    //手机端分享
    appShare: function () {
      let title = "我的成就";
      let shareUrl = `${window.location.href.split("#")[0]}#/member/myAchievement/AchievementList?refereeId=${localStorage.getItem("userId")}`;
      utilJs.appShareTrue(title, "", shareUrl, "");
    }
  },
  activated() {
    setDocumentTitle("我的证书");// eslint-disable-line
    this.showCertificate = false;
    this.certificateInfo = {};
    // this.getMyCert();
  },
  deactivated() { }
};
</script>
